<?php
namespace app\referral\controller;

use tp5_lib\api\controller\ApiBaseController;
use tp5_lib\referral\logic\Referral;
use tp5_lib\user\model\UserModel;

class ApiController extends ApiBaseController
{
    /**
     * 获取推荐用户注册的链接和二维码
     */
    public function url()
    {
        $user=$this->checkLogin();
        $domain=$this->request->domain();
        $url=$domain.url("user/login/register");
        $url=Referral::url($url,$user);
        $this->result([
            "url"=>$url,
            "qr_code"=>$domain.url("index/qrcode",["uid"=>$user["id"]]),
        ],1);
    }
    /**
     * 获取用户的推荐人
     */
    public function referrer()
    {
        $user=$this->checkLogin();
        if(empty($user["referral"])){
            $this->result([],1,"用户没有推荐人");
        }
        $model=new UserModel();
        if(!$record=$model->db()->where(["id"=>$user["referral"]])->find()){
            $model->db()->where(["id"=>$user["id"]])->setField("referral",0);
            $this->result([],1,"用户没有推荐人");
        }
        $this->result($record,1);
    }
    /**
     * 获取用户推荐的人
     */
    public function referrals()
    {
        $user=$this->checkLogin();
        $model=new UserModel();
        $record=$model->db()->where(["referral"=>$user["id"]])->select();
        $this->result($record,1);
    }
}
